package com.woniu.product.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.PageResult;
import com.woniu.product.mapper.ProductMapper;
import com.woniu.product.pojo.Product;
import com.woniu.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.product.entity.dto.DisplayProductByConditionDTO;
import com.woniu.product.entity.dto.ProductSaleSortByUserDTO;
import com.woniu.product.pojo.Product;
import com.woniu.product.mapper.ProductMapper;
import com.woniu.product.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author linxin
 * @since 2023-02-16
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired(required = false)
    private ProductMapper productMapper;

    @Override
    public PageResult<Product> querySearch(Integer currentPage, Integer pageSize, String productName) {
        // 1. 构建查询条件
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();

        wrapper.like(!StringUtils.isEmpty(productName),Product::getProductName, productName);
        // 2. 构建分页对象
        IPage<Product> page = new Page<>(currentPage, pageSize);
        // 3. 调用mapper查询数据
        IPage<com.woniu.product.pojo.Product> selectPage = productMapper.selectPage(page, wrapper);
        // 4. 封装pageResult对象
        PageResult<com.woniu.product.pojo.Product> pageResult = new PageResult<>(selectPage.getTotal(), selectPage.getRecords());
        // 5. 返回数据
        return pageResult;
    }

    @Override
    public boolean queryProductStatus(Long id) {
        boolean b = productMapper.queryProductStatus(id);
        return false;
    }

    @Override
    public List<Product> findByCategoryId(String productCategoryId){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("product_category_id",productCategoryId);
        List<Product> list = productMapper.selectList(wrapper);
        for (Product product : list) {
            System.out.println(product);
        }
        return list;
    }

    @Override
    public PageResult<Product> searchProduct(Integer currentPage, Integer pageSize, String productName) {
        //构建查询条件
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper();
        wrapper.like(!StringUtils.isEmpty(wrapper),Product::getProductName,productName);
        //构建分页参数
        IPage<Product> page = new Page<>(currentPage,pageSize);
        //mapper查询数据,需要Product不能用VO,如果需要用VO就要写Mapper写sql
        IPage<Product> selectPage = productMapper.selectPage(page,wrapper);
        //组装分页返回对象
        PageResult<Product> productPageResult = new PageResult<>(selectPage.getTotal(),selectPage.getRecords());
        return productPageResult;
    }

    @Override
    public PageResult<Product> searchProductSort(Integer currentPage, Integer pageSize, ProductSaleSortByUserDTO productSaleSortByUserDTO) {
        //构建查询条件
        QueryWrapper<Product> wrapper = new QueryWrapper();
        //第二个参数是数据库表里面用的字段名
        wrapper.like(!StringUtils.isEmpty(wrapper),"product_name" , productSaleSortByUserDTO.getProductName());
        //排序需要判断用户传入的值，是升序asc"0"还是降序desc"1"
        if (!StringUtils.isEmpty(productSaleSortByUserDTO.getSortType())){
            if ("0".equals(productSaleSortByUserDTO.getSortType())){
                wrapper.orderByAsc(productSaleSortByUserDTO.getSortName());
            }else {
                if ("1".equals(productSaleSortByUserDTO.getSortType())){
                    wrapper.orderByDesc(productSaleSortByUserDTO.getSortName());
                }
            }
        }
        //构建分页参数
        IPage<Product> page = new Page<>(currentPage,pageSize);
        //mapper查询数据,需要Product不能用VO
        IPage<Product> productIPage = productMapper.selectPage(page, wrapper);
        //组装分页返回对象
        PageResult<Product> productPageResult = new PageResult<>(productIPage.getTotal(),productIPage.getRecords());
        return productPageResult;
    }

    @Override
    public PageResult<Product> searchProductSortByCondition(Integer currentPage, Integer pageSize, DisplayProductByConditionDTO displayProductByConditionDTO) {
        //构建查询条件
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        //名字模糊查询
        wrapper.like(!StringUtils.isEmpty(wrapper),"product_name", displayProductByConditionDTO.getProductName());
        //条件查询 1.发货地查询 2.价格区间查询
        //判断是否有选发货地址和填写价格区间
        if (!StringUtils.isEmpty(displayProductByConditionDTO.getMinPrice()) && !StringUtils.isEmpty(displayProductByConditionDTO.getMaxPrice())){
            wrapper.between("price",displayProductByConditionDTO.getMinPrice(),displayProductByConditionDTO.getMaxPrice());
        }
            wrapper.eq(!StringUtils.isEmpty(wrapper),"warehouse_id",displayProductByConditionDTO.getWarehouseId());
        //构建分页参数
        IPage<Product> page = new Page<>(currentPage,pageSize);
        //mapper查询数据
        IPage<Product> productIPage = productMapper.selectPage(page,wrapper);
        //组装分页返回对象
        PageResult<Product> productPageResult = new PageResult<>(productIPage.getTotal(),productIPage.getRecords());
        return productPageResult;
    }

    @Override
    public Product selectProductById(String id) {
        Product product = productMapper.selectById(id);
        return product;
    }

    @Override
    public int updateStock(String id, Integer productQuantity) {
        return productMapper.updateStock(id,productQuantity);
    }
}
